// 页面加载时加载的方法
$(function () {
    // 通用头部搜索切换
    $("#search-bd li").click(function () {
        $(this).addClass("selected active").siblings().removeClass("selected active");
        $("#keyword").attr("placeholder", $(this).attr("placeholder"));
        $("#search_act").val($(this).attr("search_act"));
        $(".search-form").attr("action", BASE_SERVICE + "/search/" + $(this).attr("searchModel"));
    });
    // 搜索框点击搜索方法
    $('.btn-search').click(function(){
        $(".search-form").submit();
    });
    //需求发布
    $("#demandRelease").click(function () {
        location.href=BASE_SERVICE+'/myuser/demandRelease';
    });
    // 鼠标县浮加载购物车方法
    $('.hd_Shopping_list .s_cart').hover(function () {// 运行加载购物车
            load_cart_information();
        },function(){
            load_cart_information();
            //$(this).unbind('cart-hover');
        }
    );
    // 页面加载时加载购物车方法
    load_cart_information();
});

// 加载购物车方法
var load_cart_information = function() {
    $.post(BASE_SERVICE + '/cart/cart', function (data) {
        if (data.success) {
            var result = data.result;
            $('.sidebar-num').html(result.goodsNum);
            $('#shopping-amount').html(result.goodsNum);
            $('.shopping-amount').html(result.goodsNum);

            var html = '';
            html += "<div class='spacer'></div>";
            if (result.goodsNum > 0) {
                html += "<ul class='p_s_list'>";
                var i = 0;
                var j = 0;
                var data = result['list'];
                for (i = 0; i < data.length&&i < 10; i++) {
                    html += "<li id='cart_item_" + data[i]['specId'] + "' count='" + data[i]['goodsNum'] + "'>";
                    html += "<div class='img'><img src='" + IMGSERVER + data[i]['goodsImages'] + "' title='" + data[i]['goodsName'] + "'></div>";
                    html += "<div class='content'>";
                    html += "<p class='p_s_tip'><a href='" + BASE_SERVICE + "/product/" + data[i]['specId'] + ".html'>" + data[i]['goodsName'] + "</a></p>";
                    html += "<p class='p_s_tip'>" + data[i]['specInfo'] + "</p>";
                    html += "</div>";
                    html += "<div class='Operations'>";
                    html += "<p class='Price' style='background:rgba(0,0,0,0);'>&yen;" + number_format(data[i]['goodsPrice'],2) + "×" + data[i]['goodsNum'] + "</p>";
                    var goodsPackagesType = data[i]['goodsPackagesType'];
                    //alert("type"+goodsPackagesType);
                    html += "<p><a href='javascript:void(0)' onclick=\"deleteTopCart(\'" + data[i]['goodsId'] + "\',\'" + data[i]['specId'] + "\',\'" + data[i]['goodsPackagesType'] + "\');\">删除</a></p>";
                    html += "</div>";
                    var giftList =  data[i]['list'];
                    if (giftList !=null && giftList.length > 0) {
                       for (j = 0; j < giftList.length && j < 10; j++){
                           if(giftList[j]['giftNum'] > 0){
                               html += "<div class='p_s_tip'><a href='" + BASE_SERVICE + "/product/" + giftList[j]['couponSource'] + ".html'>" ;
                               html +=  "<font color='#c40000'>[赠]</font>"+giftList[j]['sourceName'] + "</a></p>";
                               html += "</div>";
                           }
                       }
                    }
                    html += "</li>";
                }
                if(data.length>10){
                    html += "<li style='text-align: center;'>●&nbsp;&nbsp;●&nbsp;&nbsp;●&nbsp;&nbsp;●&nbsp;&nbsp;●&nbsp;&nbsp;●</li>";
                }
                html += "</ul>";
                html += "<div class='Shopping_style'>";
                html += "<div class='p-total'>共<b>" + result.goodsNum + "</b>件商品　共计<strong>" + number_format(result.goodsTotalPrice,2) + "</strong></div>";
                html += "<a href='" + BASE_SERVICE + "/cart/index' id='btn-payforgoods' class='Shopping'>去购物车</a>";
                html += "</div>";
            } else {
                html = '<div class="prompts"></div><div class="nogoods"><b></b>购物车中还没有商品，赶紧选购吧！</div>';
            }
            $(".dorpdown-layer").html(html);
        }

    });
};

// 导航删除购物车信息
var deleteTopCart = function(goodsId,specId,goodsPackagesType){
    $.ajax({
        url: BASE_SERVICE + '/cart/deleteCart',
        type: 'POST',
        data: {'goodsId': goodsId, 'specId': specId,'packageType':goodsPackagesType},
        dataType: "json",
        success: function (data) {
            if(data.success){
                var result = data.result;
                if (result.goodsNum == 0) {
                    $('.sidebar-num').html('0');
                    $('.shopping-amount').html('0');

                    var html = '<div class="prompts"></div><div class="nogoods"><b></b>购物车中还没有商品，赶紧选购吧！</div>';
                    $(".dorpdown-layer").html(html);
                }
                else {
                    load_cart_information();
                }
            }
        }
    });
};

function hidePartOfMemberName(memberName) {
    return memberName.replace(/^(?:(0?1(?:3[0-9]|5[0-35-9]|7[013678]|8[0-9]|4[57]))[0-9]{5}([0-9]{3}))|(?:([.0-9_a-z-])[.0-9_a-z-]+([.0-9_a-z-]@[.0-9_a-z-]+\.[.0-9_a-z-]+))$/i, "$1$3*****$2$4");
}
